#include "animationicon.h"
#include "ui_animationicon.h"

AnimationIcon::AnimationIcon(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AnimationIcon),
    movieIconTimer(new QTimer),
    pixmapIconTimer(new QTimer),
    m_icon(QIcon(":/assets/spark.png")),
    m_movie(new QMovie(":/assets/loading.gif"))
{
    ui->setupUi(this);

    // once 500 after 180
    movieIconTimer->setInterval(500);
    // pixmapIconTimer->setInterval(0);

    connect(movieIconTimer, &QTimer::timeout, this, [=](){
        showPixmapIcon();
        movieIconTimer->setInterval(180);
    });
    connect(pixmapIconTimer, &QTimer::timeout, this, [=](){

    });
}

AnimationIcon::~AnimationIcon()
{
    delete ui;
}

/**
 * @brief AnimationIcon::showMovieIcon
 * @note
 */
void AnimationIcon::showMovieIcon()
{
    movie()->start();
    ui->icon->setPixmap(QPixmap());
    ui->icon->setMovie(movie());
    movieIconTimer->start();
}

/**
 * @brief AnimationIcon::showPixmapIcon
 * @note
 *
 */
void AnimationIcon::showPixmapIcon()
{
    movie()->stop();
    ui->icon->setPixmap(icon().pixmap(48));
    movieIconTimer->stop();
}

QIcon AnimationIcon::icon() const
{
    return m_icon;
}

void AnimationIcon::setIcon(const QIcon &newIcon)
{
//    if (m_icon == newIcon)
//        return;
    m_icon = newIcon;
    emit iconChanged();
}

QMovie *AnimationIcon::movie() const
{
    return m_movie;
}

void AnimationIcon::setMovie(QMovie *newMovie)
{
    if (m_movie == newMovie)
        return;
    m_movie = newMovie;
    emit movieChanged();
}
